<?php
namespace app\api\model;
use think\Model;
use think\Db;
use think\facade\Cache;

class Base extends Model
{
    public $sysconfig = '';//系统配置
    
    /**
    * 获取系统缓存信息
    */
    public function getConfig(){
        $config = Cache::get(config('sysconfig'));
        if(empty($config)){
            $this->setConfig();
            $config = Cache::get(config('sysconfig'));
        }
        
        $config = json_decode($config, true);
        $this->sysconfig = $config;
        return $config;
    }
    
    /**
    * 将信息写入缓存
    */
    public function setConfig(){
        $data = Db::name('set')->field('set_key as `key`, set_val as `val`')->select();
        $sys = [];
        foreach($data as $k=>$v){
            $sys[$v['key']] = $v['val'];
        }
        $value = json_encode($sys);
        
        Cache::set(config('sysconfig'),$value,3600);
    }

}